package com.shijie.controller.app;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.shijie.component.AppUserService;
import com.shijie.core.Result;
import com.shijie.core.ResultGenerator;
import com.shijie.domain.entity.Products;
import com.shijie.domain.entity.UserAttentions;
import com.shijie.domain.enums.EnumInterface;
import com.shijie.domain.enums.EnumInterface.EntityType;
import com.shijie.service.UserAttentionsService;

/*
 *  用户关注，喜欢等相关接口
 *  @author: smart boy
 *  @date: 2018-08-06
 */
@RestController
@RequestMapping("/app/attentions")
public class AppUserAttentionsController {

    @Resource
    private UserAttentionsService userAttentionsService;
    @Resource
    private AppUserService appUserService;

    /**
     * 是否已关注或喜欢
     *
     * @param id         实体类ID
     * @param entityType 实体类型
     * @return
     */
    @GetMapping(value = "/isAttention/{id}")
    public Result isAttention(@PathVariable Integer id, @RequestParam EntityType entityType) {
        UserAttentions attention = userAttentionsService.isAttentions(appUserService.getCurrentUser().getId(), entityType, id, EnumInterface.Status.VALID.ordinal());
        return ResultGenerator.success(attention != null);
    }

    /**
     * 关注(喜欢)取消关注(喜欢)
     *
     * @param id         资讯、产品、活动或者达人的ID
     * @param entityType 类型参考枚举
     * @return
     */
    @PutMapping(value = "/{id}")
	public Result like(@PathVariable Integer id, @RequestParam EntityType entityType) {
		Integer attentionNum = userAttentionsService.like(id, entityType);
		return ResultGenerator.success(attentionNum);
	}


    /**
     * 获取到关注喜欢收藏列表
     *
     * @param entityType 实体类型
     * @return
     */
    @GetMapping
    public Result attentions(@RequestParam EntityType entityType, @RequestParam(defaultValue = "0") Integer page,
                             @RequestParam(defaultValue = "10") Integer pageSize) {
        return ResultGenerator.success(userAttentionsService.attentions(appUserService.getCurrentUser().getId(), entityType,page,pageSize));
    }
}

